#include <stm32f4xx.h>
#include  "stm32f4xx_rcc.h"
#include  "stm32f4xx_gpio.h"
#include  "stm32f4xx_tim.h"

#include "global.h"
#include "buzzer.h"

/* APIs */
void buzzer_open(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;

    /* GPIO */
    RCC_AHB1PeriphClockCmd(RCC_BPR_PWM_PORT, ENABLE);
    GPIO_InitStructure.GPIO_Pin = BPR_PWM_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    //GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_Init(BPR_PWM_PORT, &GPIO_InitStructure);
    GPIO_PinAFConfig(BPR_PWM_PORT, BPR_TIM_PIN_SOURCE, GPIO_AF_TIM12);

    /* Timer */
    TIM_TimeBaseStructure.TIM_Period = 0;
    TIM_TimeBaseStructure.TIM_Prescaler = DEFAULT_PRESCALER;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(BPR_TIMER, &TIM_TimeBaseStructure);

    /* PWM1 Mode configuration: Channel2 */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 0;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC2Init(BPR_TIMER, &TIM_OCInitStructure);
    TIM_OC2PreloadConfig(BPR_TIMER, TIM_OCPreload_Enable);

    TIM_ARRPreloadConfig(BPR_TIMER,ENABLE);
    TIM_Cmd(BPR_TIMER, ENABLE);
}

void buzzer_close(void)
{
    RCC_APB1PeriphClockCmd(RCC_BPR_TIMER, DISABLE);
}

void buzzer_on(void)
{
#if 0
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = BPR_PWM_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
    //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_Init(BPR_PWM_PORT, &GPIO_InitStructure);
#endif
    TIM_Cmd(BPR_TIMER, ENABLE);
}

void buzzer_off(void)
{
#if 0
    GPIO_InitTypeDef GPIO_InitStructure;
    //
    //fix PWM_PIN in HIGH level when DISABLE PWM
    //
    GPIO_InitStructure.GPIO_Pin = BPR_PWM_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
    //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_Init(BPR_PWM_PORT, &GPIO_InitStructure);
    GPIO_ResetBits(BPR_PWM_PORT,BPR_PWM_PIN);
#endif

    TIM_Cmd(BPR_TIMER, DISABLE);
}

void buzzer_config(enum freq_t freq, enum volume_t vol)
{
    uint32_t FreqValue, VolValue;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    RCC_ClocksTypeDef  SysClk_Status_Get;
    //
    //Get periherial clock for timer
    //
    RCC_GetClocksFreq( &SysClk_Status_Get);
    FreqValue = (uint32_t)(((2*(SysClk_Status_Get.PCLK1_Frequency)/(DEFAULT_PRESCALER+1))/FrequencyTable[freq])-1);
    if (FreqValue > MAX_FREQUENCY_VALUE) {
        FreqValue = MAX_FREQUENCY_VALUE;
    }

    VolValue  = (uint32_t)(FreqValue * VolumeTable[vol] / 100);
    if (VolValue  > MAX_VOLUME_VALUE) {
        VolValue  = MAX_VOLUME_VALUE;
    }

    //
    // Set time base ( period or frequency)
    //
    TIM_TimeBaseStructure.TIM_Period = FreqValue;
    TIM_TimeBaseStructure.TIM_Prescaler = DEFAULT_PRESCALER;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(BPR_TIMER, &TIM_TimeBaseStructure);
    //
    //Set duty cycle
    //
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = VolValue;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC2Init(BPR_TIMER, &TIM_OCInitStructure);
    TIM_OC2PreloadConfig(BPR_TIMER, TIM_OCPreload_Enable);
}